<template>
  <ContentWrap>
    <fromTitle title="样品信息" :hasPermi="'qis:sample-information:create'"   :queryParams="queryParams" @search="getList" @reset="resetQuery" /> 

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >


      <el-form-item label="样品名称" prop="sampleName">
        <el-input
          v-model="queryParams.sampleName"
          placeholder="请输入样品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="样品编号" prop="sampleNum">
        <el-input
          v-model="queryParams.sampleNum"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="抽样时间" prop="samplingStartDate1">
        <el-date-picker
      
          v-model="queryParams.samplingStartDate1"
        type="daterange"
        placeholder="请选择抽样时间"
        value-format="YYYY-MM-DD HH:mm:ss"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item label="甲方单位" prop="fistParty">
        <el-input
          v-model="queryParams.fistParty"
          placeholder="请输入甲方单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="抽检状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择抽检状态"
          clearable
          class="!w-240px"
        >
          <el-option label="样品未分配" value="0" />
          <el-option label="样品已分配" value="1" />
        </el-select>
      </el-form-item>

    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
      <el-button type="warning" @click="handleAdd('create')"  v-hasPermi="['qis:sample-information:create']" >新增</el-button>
    <el-button type="info" v-hasPermi="['qis:sample-information:export']" @click="handleExport">导出</el-button>
    <el-button type="danger" v-hasPermi="['qis:sample-information:delete']" @click="handleDeleteAll">删除</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55" label="序号" align="center" type="index" />
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="样品名称" align="center" prop="sampleName" >
        <template #default="scope">
          <el-link type="primary" @click="handleEdit('update',scope.row.id)">
            {{ scope.row.sampleName }}
          </el-link>
        </template>
      </el-table-column>

      <el-table-column label="规格型号" align="center" prop="model" />
      <el-table-column label="样品编号" align="center" prop="sampleNum" />
      <el-table-column label="抽样地点" align="center" prop="samplingSite" />
      <el-table-column label="委托单位" align="center" prop="entrustCompany" />
      <el-table-column label="生产单位" align="center" prop="productionCompany" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="抽样日期" align="center" prop="samplingDate" />
      <el-table-column label="抽样方式" align="center" prop="samplingTypeString" />
      <el-table-column label="抽检状态" align="center" prop="statusString" />
<!-- 
      <el-table-column label="操作" align="center" prop="id" >
        <template #default="scope">
          <el-button type="danger" link @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SampleInformationForm title="" ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SampleInformationApi, SampleInformationVO } from '@/api/qis/sampleinformation'
import { useRouter } from 'vue-router'
import SampleInformationForm from './SampleInformationForm.vue'

/** 质检平台-样本信息 列表 */
defineOptions({ name: 'SampleInformation' })

const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SampleInformationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
let queryParams = reactive({
  type: 0,
  pageNo: 1,
  pageSize: 10,
  sampleName:'',
  sampleNum:'',
  samplingStartDate:'',
  samplingEndDate:'',
  fistParty:'',
  status: '',
  samplingStartDate1:[]
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    console.log(queryParams.samplingStartDate1)
    queryParams.samplingStartDate = queryParams.samplingStartDate1 ? queryParams.samplingStartDate1[0] :''
    queryParams.samplingEndDate =queryParams.samplingStartDate1 ? queryParams.samplingStartDate1[1] :''
    const data = await SampleInformationApi.getSampleInformationPage(queryParams)
    list.value = data.list
    list.value.forEach(e=>{
    e.selected  = false
    
   })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const arrayList = ref([])

const handleSelect = (row ) => {
  // console.log('删除指标要求', row)
  if(row.selected){
    arrayList.value.push(row.id)
    console.log(arrayList.value,'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(e=>e !== row.id)
    console.log(arrayList.value,'arrayList.value')
  }
}

// 删除多条数据
const handleDeleteAll = async ()=>{
    if(arrayList.value.length > 0){
      ElMessageBox.confirm('确定删除选中数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
      const stringIds =  arrayList.value.join(';')
   console.log(stringIds,'stringIds')
   await SampleInformationApi.deleteSampleInformation(stringIds)

   message.success('删除成功')
   arrayList.value = []
   getList()
  }).catch(() => {
  })

    }else {
      message.error('请选择要删除的数据')
    }
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 新增按钮操作 */
const handleAdd = (type: string,id?:number) => {
  router.push({
    path: '/inspection-info-management/sample-information/add',
    query: { type,id }
  })
}

// 修改
const handleEdit = (type: string, id?: number) => {
  router.push({
    path: '/inspection-info-management/sample-information/add',
    query: { type, id }
  })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SampleInformationApi.deleteSampleInformation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  if(total.value > 10000) return message.error('当前数据过大，请筛选数据后导出')
  try {
if(arrayList.value.length > 0){
  const idsArr = arrayList.value.join(';')   
        // 导出的二次确认
        await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SampleInformationApi.exportSampleInformation({ids:idsArr,...queryParams})
    download.excel(data, '质检平台-样本信息.xls')
}else {
  await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SampleInformationApi.exportSampleInformation(queryParams)
    download.excel(data, '质检平台-样本信息.xls')
}

  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


<style lang="scss" scoped>

.table-btn{
  padding: 10px 50px;
}
</style>